//
//  MESexViewController.swift
//  aj
//
//  Created by leeqb on 15/11/29.
//  Copyright © 2015年 powerlee. All rights reserved.
//

import UIKit

class MESexViewController: AJBaseTableViewController {
    var sexArray = ["男", "女", "保密"] // 性别（0-男，1女，2-未设置）
    var userInfo = DataManager.userInfo()
    var prevSelectCell: UITableViewCell?
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.navigationItem.title = "性别"
        self.tableView.zeroInsets()
        //self.tableView.selectRowAtIndexPath(NSIndexPath(forRow: 0, inSection: 0), animated: false, scrollPosition: UITableViewScrollPosition.None)
        
        let currSelectCell = tableView.cellForRowAtIndexPath(NSIndexPath(forRow: userSex, inSection: 0))
        let crossImageView = currSelectCell?.viewWithTag(2) as! UIImageView
        crossImageView.hidden = false
        prevSelectCell = currSelectCell
    }
    
    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return sexArray.count
    }
    
    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier("SelectionCell", forIndexPath: indexPath)
        cell.zeroInsets()
        
        let titleLabel = cell.viewWithTag(1) as! UILabel
        titleLabel.text = sexArray[indexPath.row]
        
        return cell
    }
    
    override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        tableView.deselectRowAtIndexPath(indexPath, animated: false)
        
        var crossImageView: UIImageView!
        if prevSelectCell != nil {
            crossImageView = prevSelectCell?.viewWithTag(2) as! UIImageView
            crossImageView.hidden = true
        }
        
        let currSelectCell = tableView.cellForRowAtIndexPath(indexPath)
        crossImageView = currSelectCell?.viewWithTag(2) as! UIImageView
        crossImageView.hidden = false
        prevSelectCell = currSelectCell
        
        let params = ["sex": "\(indexPath.row)"]
        g_myDataVC.saveProfile(params, success: { (responseObject) -> Void in
            DataManager.updateLocalUserInfo("sex", value: "\(indexPath.row)")
            }) { (error) -> Void in
        }
    }
}